//传入AI等级和单位，设置装备

params ["_unit", "_level"];

//无人机AI
if((typeof _unit) find "_UAV_AI" >= 0)exitWith{};

//平民小队，会反击玩家
//side平民武装单位返回的实际是WEST
if(side (group _unit) == civilian)then
{
	//systemChat "doFire-1";
	_unit addEventHandler["killed",{
		//systemChat "doFire0";
		if(isPlayer (_this select 1))then{
			{
				_x doFire (_this select 1);
				//systemChat "doFire!";
			}forEach (units (_this select 0));
		};
	}];
};

private _isCiv = (side _unit == civilian);

//非武装单位 不掉落东西（比如地图）
if(_isCiv
&& {as_civ_kill_num < Param_CivNum}) then{
//平民可以救人（非武装，side返回civ的只有平民单位）
	_unit call fn_initUnitMedic;
};

//平民概率终止接下来操作
if(_isCiv
&& {random 100 > Param_CivilianWeapon}) exitWith{};

if(isNil "_level") then{_level = 3};

//额外RGO手雷
if(Param_UnitsMiniGrenade > 0) then
{
	for "_i" from 1 to Param_UnitsMiniGrenade do
	{
		_unit addItem "MiniGrenade";
	};
};

if(Param_UnitsWeapon == 1
|| {side _unit == civilian}) then
{//随机武器，或者是平民
	//移除弹夹
	private _listMag = primaryWeaponMagazine _unit;
	{
		_unit removeMagazines _x;
	}forEach _listMag;

	//移除 主武器
	private _weapon = primaryWeapon _unit;
	if(_weapon != "") then
	{
		_unit removeWeaponGlobal _weapon;
	};



	if(_level == 0) then
	{
		//头盔
		removeHeadgear _unit;
		_unit addHeadgear (selectRandom ash_ai_0);
		//武器
		[0, _unit] call fn_initWeapon;
		//配件
		_unit addPrimaryWeaponItem (selectRandom selectRandom asp_ai_0);
	};

	if(_level == 1) then
	{
		//头盔
		removeHeadgear _unit;
		_unit addHeadgear (selectRandom ash_ai_1);
		//武器
		[1, _unit] call fn_initWeapon;
		//配件
		_unit addPrimaryWeaponItem (selectRandom selectRandom asp_ai_1);
	};

	if(_level == 2) then
	{
		//头盔
		removeHeadgear _unit;
		_unit addHeadgear (selectRandom ash_ai_2);
		//武器
		[2, _unit] call fn_initWeapon;
		//配件
		_unit addPrimaryWeaponItem (selectRandom selectRandom asp_ai_2);
	};

	if(_level >= 3) then
	{
		//头盔
		removeHeadgear _unit;
		_unit addHeadgear (selectRandom ash_ai_3);
		//武器
		[3, _unit] call fn_initWeapon;
		//配件
		_unit addPrimaryWeaponItem (selectRandom selectRandom asp_ai_3);
	};

};

//平民不添加之后的逻辑
if(_isCiv) exitWith{};


//不是红军
if(side _unit != east) then
{
	_unit addEventHandler ["HandleDamage", {
		params ["_unit", "_selection", "_damage", "_source", "_projectile", "_hitIndex", "_instigator", "_hitPoint"];
		//hint str typeof _unit;
		//伤害过高，原值
		if(_damage >= 1.0
		|| {!isPlayer _source}) exitWith{};
		
		//已经倒地近距离直接杀死
		if(lifeState _unit == "INCAPACITATED") exitWith{
			if(_unit distanceSqr _source < 25
				&& {random 100 < 50})then{
				//近距离直接斩杀
				1
			};
		};
		//倒地再给机会
		if(_damage >= (1.0 - damage _unit)) exitWith
		{
			//播放倒地音效
			if((_unit getVariable ["as_speak_time", 0]) + 5 < time) then
			{
				[_unit, selectRandom ast_down] remoteExec ["say3D", 0];
				_unit setVariable ["as_speak_time", time];
			};
			//倒地流血
			if(!(_unit getVariable ["as_ai_down", false]))then{
				_unit setVariable ["as_ai_down", true];
				_unit setUnconscious true;
				[_unit, _source] spawn fn_aiDown;
			};
			0
		};
		
		//不是致命伤，播放普通音效
		if((_unit getVariable ["as_speak_time", 0]) + 5 < time) then
		{
			[_unit, selectRandom ast_pain] remoteExec ["say3D", 0];
			_unit setVariable ["as_speak_time", time];
		};
		
		//按默认处理
	}];
}else
{//红军AI禁止触发动态模拟
	_unit triggerDynamicSimulation false;
};

//GF效果
_unit remoteExec ["fn_initUnitLocal", 0];

//死亡掉落
_unit addEventHandler ["killed","_this call fn_onUnitDead"];

//地雷专家检测
[_unit] call fn_runSupportMine;

//筒子兵检测
[_unit] spawn fn_runSupportAT;

//UAV检测
[_unit] spawn fn_runSupportUAV;
